package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ClassTime;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.entity.RoomTime;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.RoomQueryVo;
import com.yuyou.fn.platform.entity.SysUser;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by xxx on 2017/3/13
 */

@Controller
@RequestMapping("/api/admin/arrangement")
public class AdminArrangementController extends AdminApiBaseController {

    @Resource
    private IRoomService roomService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private ITeacherService teacherService;

    @Resource
    private IClassTimeService classTimeService;

    @Resource
    private IRoomTimeService roomTimeService;


    @ApiOperation("获取可排教室列表")
    @RequestMapping(value = "/roomListForClassArrangement", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<Room>> roomListForClassArrangement(RoomQueryVo roomQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                                @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        Page<Room> page = roomService.findRoomListForClassArrangement(roomQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("保存更新班级课室")
    @RequestMapping(value = "/arrangementRoom", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> arrangementRoom(@RequestParam(value = "classId") Long classId, @RequestParam(value = "roomId") Long roomId) {

        courseClassService.saveArrangementRoom(classId, roomId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("获取可排老师列表")
    @RequestMapping(value = "/teacherListForArrangement", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ReturnResult<Page<Teacher>> teacherListForArrangement(@RequestParam(value = "busTeamId") Long busTeamId,
                                                                 @RequestParam(value = "gradeName") String gradeName,
                                                                 @RequestParam(value = "subjectName") String subjectName,
                                                                 @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                                 @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        Page<Teacher> page = teacherService.findTeacherListForClassArrangement(busTeamId, gradeName, subjectName, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("讲次列表")
    @RequestMapping(value = "ClassTimeList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ReturnResult<List<ClassTime>> classTimeList(@RequestParam(value = "classId") Long classId) {

        List<ClassTime> classTimeList = classTimeService.findClassTimeByClassId(classId);
        return ReturnResult.success(classTimeList);
    }

    @ApiOperation("排时间")
    @RequestMapping(value = "/arrangeTime", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> arrangeTime(HttpServletRequest request) {
        return ReturnResult.success("Ok");
    }

    @ApiOperation("保存更新班级老师")
    @RequestMapping(value = "/arrangeTeacher", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> arrangeTeacher(@RequestParam(value = "classId") Long classId,
                   @RequestParam(value = "teacherIds") String teacherIdsStr, HttpServletRequest request) {
        courseClassService.saveArrangementTeacher(classId, Utils.toLongIds(teacherIdsStr));
        return ReturnResult.success("Ok");
    }

    @ApiOperation("教室排课情况")
    @RequestMapping(value = "/roomTimeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Map<String,String>> roomTimeList(@RequestParam(value = "roomId") Long roomId , @RequestParam(value = "start") Date start , @RequestParam(value = "end") Date end, HttpServletRequest request) {
        SysUser user = getUser(request);
        if(end != null ){
            DateUtils.addDays(end,1);
        }
        List<RoomTime> roomTimeList = roomTimeService.findRoomTimeByRoomId(roomId, start, end ,user.getBusTeamId());
        List<Map<String,String>> retList = new ArrayList<>(roomTimeList.size());
        for(RoomTime roomTime : roomTimeList ){
            Map<String,String> m = new HashMap<>();
            m.put("title",roomTime.getSubject());
            m.put("start", TimeUtils.date2String(roomTime.getStart()));
            m.put("end", TimeUtils.date2String(roomTime.getEnd()));
            retList.add(m);
        }
        return retList;
    }

    @ApiOperation("老师排课情况")
    @RequestMapping(value = "/teacherClassTimeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Map<String,String>> teacherClassTimeList(@RequestParam(value = "teacherId") Long teacherId , @RequestParam(value = "start") Date start , @RequestParam(value = "end") Date end, HttpServletRequest request) {
        if(end != null ){
            DateUtils.addDays(end,1);
        }
        List<ClassTime> classTimeList = classTimeService.findClassTimeByTeacherId(teacherId, start, end);
        List<Map<String,String>> retList = new ArrayList<>(classTimeList.size());
        for(ClassTime classTime : classTimeList ){
            Map<String,String> m = new HashMap<>();
            m.put("title","");
            String date = TimeUtils.date2String(classTime.getClassDate(),"yyyy-MM-dd ");
            m.put("start", date + classTime.getStartTime());
            m.put("end", date + classTime.getEndTime() );
            retList.add(m);
        }
        return retList;
    }

}